/**
 * 搜索服务 - 封装搜索相关的IPC调用
 */

// 搜索服务
import type { SearchParams, SearchFilters, PhotoListResponse, AlbumListResponse, TagListResponse, PopularTag } from '../types/search'
import type { Photo } from '../types/photo'
import type { Album } from '../types/album'
import type { Tag } from '../types/common'

// 导入全局类型
import '../types/global'

/**
 * 搜索服务类
 */
export class SearchService {
  /**
   * 搜索照片
   * @param params 搜索参数
   * @returns 搜索结果
   */
  static async query(params: SearchParams): Promise<PhotoListResponse> {
    const response = await window.electronAPI.search.query(params)
    
    if (!response.success) {
      throw new Error(response.error || '搜索照片失败')
    }
    
    return response.data
  }

  /**
   * 获取搜索建议
   * @param query 查询字符串
   * @returns 搜索建议
   */
  static async getSuggestions(query: string): Promise<{ suggestions: string[] }> {
    const response = await window.electronAPI.search.getSuggestions({ type: 'all', query })
    
    if (!response.success) {
      throw new Error(response.error || '获取搜索建议失败')
    }
    
    return response.data
  }

  /**
   * 搜索照片
   * @param params 搜索参数
   * @returns 搜索结果
   */
  static async searchPhotos(params: {
    query?: string
    page?: number
    pageSize?: number
    filters?: SearchFilters
  }): Promise<PhotoListResponse> {
    const response = await window.electronAPI.search.photos(params)
    
    if (!response.success) {
      throw new Error(response.error || '搜索照片失败')
    }
    
    return response.data
  }

  /**
   * 搜索相册
   * @param params 搜索参数
   * @returns 搜索结果
   */
  static async searchAlbums(params: {
    query?: string
    page?: number
    pageSize?: number
  }): Promise<{ albums: any[] }> {
    const response = await window.electronAPI.search.albums(params)
    
    if (!response.success) {
      throw new Error(response.error || '搜索相册失败')
    }
    
    return response.data
  }

  /**
   * 搜索标签
   * @param params 搜索参数
   * @returns 搜索结果
   */
  static async searchTags(params: {
    query?: string
    page?: number
    pageSize?: number
  }): Promise<{ tags: any[] }> {
    const response = await window.electronAPI.search.tags(params)
    
    if (!response.success) {
      throw new Error(response.error || '搜索标签失败')
    }
    
    return response.data
  }

  /**
   * 综合搜索
   * @param params 搜索参数
   * @returns 搜索结果
   */
  static async search(params: {
    query?: string
    includePhotos?: boolean
    includeAlbums?: boolean
    includeTags?: boolean
    page?: number
    pageSize?: number
    filters?: SearchFilters
  }): Promise<{
    photos?: Photo[]
    albums?: any[]
    tags?: any[]
    page: number
    pageSize: number
    total: number
    totalPages: number
  }> {
    const response = await window.electronAPI.search.query(params)
    
    if (!response.success) {
      throw new Error(response.error || '综合搜索失败')
    }
    
    return response.data
  }

  /**
   * 获取热门标签
   * @param limit 限制数量
   * @returns 热门标签列表
   */
  static async getPopularTags(limit: number = 10): Promise<{ tags: PopularTag[] }> {
    const response = await window.electronAPI.search.getPopularTags({ limit })
    
    if (!response.success) {
      throw new Error(response.error || '获取热门标签失败')
    }
    
    return response.data
  }

  /**
   * 获取所有标签
   * @returns 所有标签列表
   */
  static async getAllTags(): Promise<any[]> {
    const response = await window.electronAPI.search.getAllTags()
    
    if (!response.success) {
      throw new Error(response.error || '获取标签列表失败')
    }
    
    return response.data
  }

  /**
   * 获取相机制造商列表
   * @returns 相机制造商列表
   */
  static async getCameraMakes(): Promise<{ makes: string[] }> {
    const response = await window.electronAPI.search.getCameraMakes()
    
    if (!response.success) {
      throw new Error(response.error || '获取相机制造商失败')
    }
    
    return response.data
  }

  /**
   * 获取相机型号列表
   * @param make 相机品牌
   * @returns 相机型号列表
   */
  static async getCameraModels(make?: string): Promise<{ models: string[] }> {
    const response = await window.electronAPI.search.getCameraModels({ make })
    
    if (!response.success) {
      throw new Error(response.error || '获取相机型号失败')
    }
    
    return response.data
  }
}